package cn.controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.common.ExceResult;
import cn.common.LayTable;
import cn.filter.LoginCheck;
import cn.kpi.common.mapper.JsonMapper;
import cn.kpi.common.utils.DateFormatUtils;
import cn.kpi.entity.attendance.AttRoutineEntity;
import cn.kpi.entity.attendance.AttSummaryEntity;
import cn.kpi.service.attendance.IAttRoutineService;

@Controller
@RequestMapping("attroutine")
public class AttRoutineController extends BaseController {

	@Autowired
	private IAttRoutineService iAttRoutineService;

	@RequestMapping(value = "/att_routine_list")
	public String AttRoutineList(Model model) {
		try {
			Calendar cale = Calendar.getInstance();
			int year = cale.get(Calendar.YEAR);
			model.addAttribute("year", year);

		} catch (Exception ex) {

		}
		return "attendance/att_routine_list";
	}

	@RequestMapping(value = "/att_routine_day_edit")
	public String AttRoutineDayEdit(Model model, Long routineid) {
		try {
			Calendar cale = Calendar.getInstance();
			int year = cale.get(Calendar.YEAR);
			model.addAttribute("year", year);
			model.addAttribute("routineid", routineid);
			AttRoutineEntity entity=iAttRoutineService.GetAttRoutineListById(routineid);
			model.addAttribute("entity", entity);
		} catch (Exception ex) {

		}
		return "attendance/att_routine_day_edit";
	}
	
	@RequestMapping(value = "/att_routine_week_edit")
	public String AttRoutineWeekEdit(Model model, Long routineid) {
		try {
			Calendar cale = Calendar.getInstance();
			int year = cale.get(Calendar.YEAR);
			model.addAttribute("year", year);
			model.addAttribute("routineid", routineid);
			AttRoutineEntity entity=iAttRoutineService.GetAttRoutineListById(routineid);
			model.addAttribute("entity", entity);
		} catch (Exception ex) {

		}
		return "attendance/att_routine_week_edit";
	}
	
	@RequestMapping(value = "/att_routine_mouth_edit")
	public String AttRoutineMouthEdit(Model model, Long routineid) {
		try {
			Calendar cale = Calendar.getInstance();
			int year = cale.get(Calendar.YEAR);
			model.addAttribute("year", year);
			model.addAttribute("routineid", routineid);
			AttRoutineEntity entity=iAttRoutineService.GetAttRoutineListById(routineid);
			model.addAttribute("entity", entity);
		} catch (Exception ex) {

		}
		return "attendance/att_routine_mouth_edit";
	}

	@RequestMapping(value = "/add_attroutine")
	public String AddAttRoutine(HttpServletRequest request, HttpServletResponse response) {
		try {
			AttRoutineEntity entity = new AttRoutineEntity();
			entity.setRoutineid(Long.parseLong(request.getParameter("routineid")));
			entity.setUserid(this.GetLoginUserId());
			entity.setYear(Integer.parseInt(request.getParameter("year")));
			entity.setMouth(Integer.parseInt(request.getParameter("mouth")));
			entity.setRoutinedes(request.getParameter("routinedes"));
			entity.setRoutinetype(Integer.parseInt(request.getParameter("routinetype")));
			// entity.setRemark(request.getParameter("remark"));
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			if (entity.getRoutinetype() == 0) {// 日纪实
				entity.setDay(Integer.parseInt(request.getParameter("day")));
				entity.setBegintime(sdf.parse(entity.getYear() + "-" + entity.getMouth() + "-" + entity.getDay()));
				entity.setEndtime(sdf.parse(entity.getYear() + "-" + entity.getMouth() + "-" + entity.getDay()));
			} else if (entity.getRoutinetype() == 1) {// 周汇总
				entity.setWeek(Integer.parseInt(request.getParameter("week")));
				Date begin = DateFormatUtils.getFirstOfWeek(String.valueOf(entity.getYear()),
						String.valueOf(entity.getMouth()), entity.getWeek());
				Date end = DateFormatUtils.getLastOfWeek(String.valueOf(entity.getYear()),
						String.valueOf(entity.getMouth()), entity.getWeek());
				entity.setBegintime(begin);
				entity.setEndtime(end);
			} else if (entity.getRoutinetype() == 2) {// 月汇总
				entity.setBegintime(sdf.parse(entity.getYear() + "-" + entity.getMouth() + "-01"));
				Date end = DateFormatUtils.getLastOfMouth(String.valueOf(entity.getYear()),
						String.valueOf(entity.getMouth()));
				entity.setEndtime(end);
			}
			JsonMapper map = new JsonMapper();
			System.out.println(map.toJson(entity));
			int iResult = 0;
			if (entity.getRoutineid() > 0) {
				iResult = iAttRoutineService.UpdateAttRoutine(entity);
			} else {
				iResult = iAttRoutineService.InsertAttRoutine(entity);
			}

			return renderStringScript(response, "<script>window.parent.CloseDialog(" + iResult + ")</script>");
		} catch (Exception ex) {
			ex.printStackTrace();
			return renderStringScript(response, "<script>window.parent.CloseDialog(0)</script>");
		}
	}

	@LoginCheck(description = true)
	@ResponseBody
	@RequestMapping(value = "/get_list")
	public LayTable<AttRoutineEntity> GetList(HttpServletRequest request) {

		List<AttRoutineEntity> lst = null;
		LayTable<AttRoutineEntity> layTable = new LayTable<AttRoutineEntity>();
		try {
			String year = request.getParameter("year");
			String mouth = request.getParameter("mouth");
			Integer imonth=Integer.parseInt(mouth);
			String type = request.getParameter("routinetype");

			String where = " AND userid="+this.GetLoginUserId()+" AND routinetype=" + type;
			if (year != null && year.length() > 0) {
				where += " AND year=" + year;
			}
			if (mouth != null && mouth.length() > 0&&imonth>0) {
				where += " AND mouth=" + mouth;
			}

//			if (type.equals("0")) {
//				String day = request.getParameter("day");
//				if (day != null && day.length() > 0) {
//					where += " AND day=" + day;
//				}
//			} else if (type.equals("0")) {
//				String week = request.getParameter("week");
//				if (week != null && week.length() > 0) {
//					where += " AND week=" + week;
//				}
//			}

			lst = iAttRoutineService.GetAttRoutineListByWhere(where);
//			for (int i = 0; i < lst.size(); i++) {
//				int rowindex=
//				lst.get(i).setRowNum(i + 1);
//			}

			int iCount = iAttRoutineService.GetAttRoutineListCountByWhere(where);
			layTable.setCode(0);
			layTable.setCount(iCount);
			layTable.setData(lst);

		} catch (Exception e) {
			layTable.setCode(500);
		}
		return layTable;
	}
	
	@ResponseBody
	@RequestMapping(value = "/delete_attroutine")
	public ExceResult DeleteAttRoutine(HttpServletRequest request,HttpServletResponse response){
		ExceResult result=new ExceResult();
		result.setCode(400);
		result.setResult(0);
		try{
		 Long routineid=Long.parseLong(request.getParameter("routineid"));
		 int iResult=iAttRoutineService.DeteleAttRoutine(routineid);
		 if(iResult>0){
				result.setCode(200);
				result.setResult(iResult);
				result.setMsg("操作成功");
			}
			else{
				result.setCode(300);
				result.setResult(iResult);
				result.setMsg("操作失败");
			}
		}
		catch(Exception ex){
			result.setCode(500);
			result.setResult(0);
			result.setMsg("操作异常");
		}
		return result;
	}

}
